package com.mvp.shq.washer.view.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.adapter.ShopSpecAdapter;
import com.mvp.shq.washer.entity.shop.Product;
import com.mvp.shq.washer.entity.shop.ShopSpec;
import com.mvp.shq.washer.utils.ImageUtil;
import com.mvp.shq.washer.utils.StringUtils;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by shq1 on 2017/10/9.
 */

public class SpecPopupWindow extends PopupWindow {


    private View mPopView;
    private Activity activity;
    private ViewHolder holder;
    private List<ShopSpec> data;
    private ShopSpecAdapter adapter;
    private ShopSpecAdapter.OnAttrClickListener listener;
    private Product product;
    private ImageUtil imageUtil;
    private OnBtnClickListener btnListener;


    public SpecPopupWindow(Context context, Activity mActivity, List<ShopSpec> list, ShopSpecAdapter.OnAttrClickListener clickListener,OnBtnClickListener btnClickListenerb) {
        super(context);
        imageUtil = new ImageUtil(context);
        activity = mActivity;
        data = list;
        listener = clickListener;
        btnListener = btnClickListenerb;
        // TODO Auto-generated constructor stub
        init(context);
        setPopupWindow();
    }

    /**
     * 初始化
     *
     * @param context
     */
    @SuppressLint("WrongViewCast")
    private void init(Context context) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = LayoutInflater.from(context);
        //绑定布局
        mPopView = inflater.inflate(R.layout.pop_product_spec, null);
        holder = new ViewHolder(mPopView);
        holder.recyclerview.setLayoutManager(new LinearLayoutManager(context));
        adapter = new ShopSpecAdapter(context, data, new ShopSpecAdapter.OnAttrClickListener() {

            @Override
            public void onClick(int specPosition, int attrPosition) {
                listener.onClick(specPosition,attrPosition);
            }
        });
        holder.recyclerview.setAdapter(adapter);
        SpecPopupWindow that = this;
        holder.imgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                that.dismiss();
            }
        });
        holder.btnAddtoCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnListener.onAddtoCartClick();
            }
        });
        holder.btnBuyNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnListener.onBuyNowClick();
            }
        });
        holder.imgIncrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = Integer.parseInt(holder.etNum.getText().toString());
                num ++;
                btnListener.onNumChange(num);
                holder.etNum.setText(num+"");
            }
        });
        holder.imgReduced.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = Integer.parseInt(holder.etNum.getText().toString());
                num --;
                if (num < 1){
                    num = 1;
                }
                btnListener.onNumChange(num);
                holder.etNum.setText(num+"");
            }
        });
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                lp.alpha = 1f;
                activity.getWindow().setAttributes(lp);
            }
        });

    }

    /**
     * 设置窗口的相关属性
     */
    @SuppressLint("InlinedApi")
    private void setPopupWindow() {
        this.setContentView(mPopView);// 设置View
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);// 设置弹出窗口的宽
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的高
        this.setFocusable(true);// 设置弹出窗口可
        this.setAnimationStyle(R.style.popwindow_anim_bottom);// 设置动画
        this.setBackgroundDrawable(new BitmapDrawable());// 设置背景透明
        //this.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mPopView.setOnTouchListener(new View.OnTouchListener() {// 如果触摸位置在窗口外面则销毁
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                int height = mPopView.findViewById(R.id.id_pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });


    }

    public void setProduct(Product p) {
        this.product = p;
        BigDecimal decimal = new BigDecimal("0");
        int type = 0;
        String money1 = "",money2 = "",integral = "";
        if (product.getCashPrice() > 0 && product.getIntegralPrice() > 0){
            type = 0;
            money1 = (int)product.getCashPrice()+"";
            money2 = StringUtils.BigDecimalToStr(BigDecimal.valueOf(product.getCashPrice()));
            integral = (int)product.getIntegralPrice()+"";
        }else if (product.getCashPrice() > 0){
            type = 2;
            money1 = (int)product.getCashPrice()+"";
            money2 = StringUtils.BigDecimalToStr(BigDecimal.valueOf(product.getCashPrice()));
        }else {
            type = 1;
            integral = (int)product.getIntegralPrice()+"";
        }
        switch (type){
            case 0:
                //现金加积分购买
                holder.llMoney.setVisibility(View.VISIBLE);
                holder.tvAdd.setVisibility(View.VISIBLE);
                holder.llIntegral.setVisibility(View.VISIBLE);
                holder.tvIntegralPrompt.setVisibility(View.GONE);
                holder.tvMoney1.setText(money1);
                holder.tvMoney2.setText(money2);
                holder.tvIntegral.setText(integral);
                break;
            case 1:
                //积分购买
                holder.llMoney.setVisibility(View.GONE);
                holder.tvAdd.setVisibility(View.GONE);
                holder.llIntegral.setVisibility(View.VISIBLE);
                holder.tvIntegralPrompt.setVisibility(View.VISIBLE);
                holder.tvIntegral.setText(integral);
                break;
            case 2:
                //现金购买
                holder.llMoney.setVisibility(View.VISIBLE);
                holder.tvAdd.setVisibility(View.GONE);
                holder.llIntegral.setVisibility(View.GONE);
                holder.tvIntegralPrompt.setVisibility(View.GONE);
                holder.tvMoney1.setText(money1);
                holder.tvMoney2.setText(money2);
                break;
        }
        imageUtil.loadUrlImage(product.getImgUrl(),holder.imgProductImg);
    }

    public interface OnBtnClickListener{
        void onAddtoCartClick();
        void onBuyNowClick();
        void onNumChange(int num);
    }

    public class ViewHolder{
        @BindView(R.id.img_product_img)
        ImageView imgProductImg;
        @BindView(R.id.img_close)
        ImageView imgClose;
        @BindView(R.id.tv_money_1)
        TextView tvMoney1;
        @BindView(R.id.tv_money2)
        TextView tvMoney2;
        @BindView(R.id.ll_money)
        LinearLayout llMoney;
        @BindView(R.id.tv_add)
        TextView tvAdd;
        @BindView(R.id.tv_integral)
        TextView tvIntegral;
        @BindView(R.id.ll_integral)
        LinearLayout llIntegral;
        @BindView(R.id.tv_integral_prompt)
        TextView tvIntegralPrompt;
        @BindView(R.id.ll_price)
        LinearLayout llPrice;
        @BindView(R.id.tv_spec_des)
        TextView tvSpecDes;
        @BindView(R.id.recyclerview)
        RecyclerView recyclerview;
        @BindView(R.id.btn_add_to_cart)
        Button btnAddtoCard;
        @BindView(R.id.btn_buy_now)
        Button btnBuyNow;
        @BindView(R.id.img_increase)
        ImageView imgIncrease;
        @BindView(R.id.img_reduced)
        ImageView imgReduced;
        @BindView(R.id.et_num)
        EditText etNum;
        public ViewHolder(View itemView){
            ButterKnife.bind(this,itemView);
        }
    }
}
